<?php

namespace app\user\controller;
use app\common\controller\UserBase;
use app\common\model\Customer as Kehu;
use app\common\model\CustomerTrack;
use app\common\model\CustomerRemind;
use app\common\model\UserConfig;

class Config extends UserBase
{
    protected function _initialize()
    {
        parent::_initialize();
        !config('website_status') && die(config('colse_explain'));
        $config = cache('db_config_data');
        if (!$config) {
            $config = [];
            foreach (model('config')->select() as $v) {
                $config[$v['group']][$v['name']] = $v['value'];
            }
            cache('db_config_data', $config);
        }
        config($config);
    }
    
    //参数设置
    public function index()
    {
        default_config('customer');//默认客户参数
        default_config('track');//默认跟踪参数
        $where = "";
        $list = model('user_config')->order('sort_order asc,id asc')->where($where)->paginate(config('page_number'));
        
        return $this->fetch(is_Client('config'), ['list' => $list]);
    }
    //编辑参数
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = session('user_auth.shop_id');
            $result = $this->validate($param, 'userconfigadd');
            if ($result !== true) {
                $this->error($result);
            }
            if ($this->insert('user_config', $param) === true) {
                insert_user_log('添加了基本配置');
                $this->success('添加成功', url('user/config/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch(is_Client('edit_config'));
    }
    //编辑参数
    public function edit()
    {
        if ($this->request->isPost()) {
            if ($this->update('user_config', $this->request->param(), input('_verify', true)) === true) {
                insert_user_log('修改了基本配置');
                $this->success('修改成功', url('user/config/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch(is_Client('edit_config'), ['data' => model('user_config')->where('id', input('id'))->find()]);
    }
    //删除参数
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $uconfig = UserConfig::get($param['id']);
            if($uconfig['is_sys'] == '1'){
                $this->error("系统关键字段禁止删除");
            }
            if ($this->delete('user_config', $this->request->param()) === true) {
                insert_user_log('删除了基本配置');
                $this->success('删除成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
    }

    
}
